<template>
  <div style="width: 70%; margin: 5px auto">
    <div class="search" style="margin: 20px 0px">
      <el-input placeholder="请输入课程名称" style="width: 200px" v-model="name"></el-input>
      <el-button type="info" plain style="margin-left: 10px" @click="load(1)">查询</el-button>
      <el-button type="warning" plain style="margin-left: 10px" @click="reset">重置</el-button>
    </div>
    <div class="table">
      <el-table :data="tableData">
        <el-table-column prop="img" label="课程封面" align="center">
          <template v-slot="scope">
            <div style="display: flex;align-items: center">
              <el-image style="width: 400px; height: 160px; border-radius: 5px" v-if="scope.row.img"
                        :src="scope.row.img" :preview-src-list="[scope.row.img]"></el-image>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="课程名称" show-overflow-tooltip align="center">
          <template v-slot="scope">
            <a :href="'/front/scoreDetail?id=' + scope.row.id" style="color: #448231; font-size: large;">{{ scope.row.name }}</a>
          </template>
        </el-table-column>
        <el-table-column prop="type" label="课程类型" width="120">
          <template v-slot="scope">
            <span v-if="scope.row.type === 'VIDEO'" style="color: #b67259; font-size: large;">视频课</span>
            <span v-else style="color: #448231; font-size: large;">图文课</span>
          </template>
        </el-table-column>
        <el-table-column prop="price" label="课程积分" align="center" width="120">
          <template v-slot="scope">
            <span style="color: red; font-size: large;" v-if="scope.row.price > 0"> {{ scope.row.price }} 积分</span>
            <span v-else style="color: green; font-size: large;">公开课</span>
          </template>
        </el-table-column>
        <el-table-column prop="time" label="发布时间"  align="center" width="150">
          <template v-slot="scope">
            <span style="color: green; font-size: medium;">{{ formatDate(scope.row.time) }}</span>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
            background
            @current-change="handleCurrentChange"
            :current-page="pageNum"
            :page-sizes="[5, 10, 20]"
            :page-size="pageSize"
            layout="total, prev, pager, next"
            :total="total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>
<script>

export default {

  data() {
    return {
      user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
      tableData: [],  // 所有的数据
      pageNum: 1,   // 当前的页码
      pageSize: 4,  // 每页显示的个数
      total: 0,
      name: null,
    }
  },
  mounted() {
    this.load(1)
  },
  // methods：本页面所有的点击事件或者其他函数定义区
  methods: {
    load(pageNum) {  // 分页查询
      if (pageNum) this.pageNum = pageNum
      this.$request.get('/score/selectPage', {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          name: this.name,
        }
      }).then(res => {
        this.tableData = res.data?.list
        this.total = res.data?.total
      })
    },
    reset() {
      this.name = null
      this.load(1)
    },
    handleCurrentChange(pageNum) {
      this.load(pageNum)
    },
  },
  computed: {
    formatDate() {
      // 假设 scope.row.time 是从父组件通过 props 传递的
      return (time) => {
        const date = new Date(time);
        const year = date.getFullYear();
        const month = date.getMonth() + 1; // getMonth() 返回的月份从0开始，所以加1
        const day = date.getDate();
        return `${year}年${month}月${day}日`;
      };
    }
  },
}
</script>
